home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / keyin / strstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  2.8 KB  |  120 lines

  1. /*  string search in string as if standerd C library */
  2. /*                                      (c) Shinwa   */
  3. /*     release note:                                 */
  4. /*                    1992/12/8  first release       */
  5. /*          Permission to use, copy, distribute, this software is garanted */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. #define ON (1)
  13. #define OFF (0)
  14.  
  15. char *Myname;    /* This command name set in main() */
  16.  
  17. struct {            /* command line options */
  18.     int o : 1;
  19.     int t : 1;
  20.     int v : 1;
  21. } Option = {OFF, ON, OFF};        /* default option */
  22.  
  23. void help(void)
  24. {
  25.     if (Option.v) {
  26.         puts(" find string in source and return error code. ver 1.0 93/2/26");
  27.     }
  28.     printf("usage %s [-otvh] source string\n", Myname);
  29.     if (Option.v) {
  30.         puts(" -o, -t select case of string Two or One case");
  31.         puts(" -h     print this message");
  32.         puts(" -v     message more wordly");
  33.         puts("            by Shiwa");
  34.         puts("             Permission to use, copy, distribute, this software is garanted");
  35.     }
  36. }
  37.  
  38. char *tolower_str(char *s)
  39. {
  40.     char    *p;
  41.  
  42.     for (p=s; *p != '\0'; p++)
  43.         *p = tolower(*p);
  44.     return s;
  45. }
  46.  
  47.  
  48. int main(int argc, char **argv)
  49. {
  50.     char *source;    /* source string */
  51.     char *string;    /* find string in source */
  52.     char *p;        /* pointer to string in source */
  53.     int i, j;
  54.     
  55.     Myname = argv[0];
  56.     source = string = NULL;        /* check whether set or not */
  57.     for (i=1; i < argc; i++) {
  58.         if (argv[i][0] == '-' || argv[i][0] == '/') {
  59.             for (j=1; argv[i][j] != '\0'; j++) {
  60.                 if (argv[i][j] == 'o') {
  61.                     Option.o = ON;
  62.                     Option.t = OFF;
  63.                 } else if (argv[i][j] == 't') {
  64.                     Option.t = ON;
  65.                     Option.o = OFF;
  66.                 } else if (argv[i][j] == 'v') {
  67.                     Option.v = ON;
  68.                 } else if (argv[i][j] == 'h' || argv[i][j] == '?') {
  69.                     Option.v = ON;
  70.                     help();
  71.                     exit(EXIT_SUCCESS);
  72.                 } else {
  73.                     fprintf(stderr, "%s: Option error!\n", Myname);
  74.                     help();
  75.                     exit(EXIT_FAILURE);
  76.                 }
  77.             }
  78.         } else if (source == NULL) {
  79.             source = argv[i];
  80.         } else if (string == NULL) {
  81.             string = argv[i];
  82.         } else {
  83.             fprintf(stderr, "%s: Too many option strings!\n", Myname);
  84.             help();
  85.             exit(EXIT_FAILURE);
  86.         }
  87.     }
  88.     
  89.     if (string == NULL) {
  90.         fprintf(stderr, "%s: Too few option string!\n", Myname);
  91.         help();
  92.         exit(EXIT_FAILURE);
  93.     }
  94.     if (Option.v) {
  95.         printf("find string with %s\n", (Option.t == ON) ? "Two case" : "One case ");
  96.     }
  97.     if (Option.o) {
  98.         tolower_str(source);
  99.         tolower_str(string);
  100.     }
  101.     
  102.     p = strstr(source, string);
  103.     
  104.     
  105.     if (p) {
  106.         if (Option.v) {
  107.             printf(" found %s in %s at %d\n",
  108.                      string, source, (int) (p - source + 1));
  109.         }
  110.         return (int) (p - source + 1);
  111.     } else {
  112.         if (Option.v) {
  113.             printf(" not found %s in %s\n", string, source);
  114.         }
  115.         return 0;
  116.     }
  117. }
  118.  
  119.  
  120.